package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"server/model"
	"server/model/request"
	"server/model/response"
	"server/service"
	"server/utils"
)

func DeleteRole(c *gin.Context) {

	roleID := c.Param("role_id")
	if roleID == "" {
		response.FailWithMessage("删除失败，角色ID不能为空", c)
	}

	if err := service.DeleteRole(roleID); err != nil { // 删除角色之前需要判断是否有用户正在使用此角色
		fmt.Printf("删除失败! %v\n", err)
		response.FailWithMessage("删除失败"+err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

func GetRoleInfo(c *gin.Context) {
	roleID := c.Param("role_id")
	if roleID == "" {
		//默认查询所有信息
	}
	if err, u := service.GetRoleInfo(roleID); err != nil {
		fmt.Printf("查询失败 %v", err)
		response.FailWithMessage("查询失败，找不到当前角色", c)
	} else {
		response.OkWithData(u, c)
	}

}

func CreateRole(c *gin.Context) {
	var role request.CreateRoleRequest
	_ = c.ShouldBindJSON(&role)
	if err := utils.Verify(role, utils.AuthorityVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	r := model.SysRole{RoleName: role.RoleName, RoleId: role.RoleID}
	if err, roleBack := service.CreateRole(r); err != nil {
		fmt.Printf("创建角色失败 %v", err)
		response.FailWithMessage("创建失败，角色ID已经存在", c)
	} else {
		response.OkWithDetailed(roleBack, "创建成功", c)
	}
}
